///////////////////////////////////////////////////////////////////////////////////////////////////
// DB_SQLite.h
// 2008?7?4?
// DB_SQLite header  CDB_SQLite class to wraps 8dworld database's operation
// Copyright ? 2007, 2008, 8DWorld, Inc. All rights reserved.
//
// 
///////////////////////////////////////////////////////////////////////////////////////////////////


#ifndef __DB_SQLITE_H__
#define __DB_SQLITE_H__
#include <atlstr.h>
#include "SQLite3E.h"
#include "Entry/TExportUtil.h"

#define SQLiteDBQuery CppSQLite3Query 
#define SQLiteDB      CppSQLite3DB

class TUTIL_API CSQLiteBase
{
public:
	typedef void (*LogCallBack)(int classid ,const char*);
	void SetLogCall(LogCallBack callback)
	{
		m_LogCallBack = callback;
	}

	typedef std::vector<CString> SQLiteList;
	typedef std::vector<CString>::iterator SQLiteVectorIter;	CSQLiteBase();
	~CSQLiteBase();

	bool Open(const char* FileName);
	bool OpenFullPath(const char* FileName);
	void Close();
public:
	bool BeginSqlCommit(SQLiteList& test);
	void SetWorkDirectory(const char* DirectoryName);
	void SetLiteLogName(const char* LogName); 
	void WriteSQLLog(int i , const char* szLogError);
	bool IsOpen();

	//this method may be override, because this method just for utf8-to ansi.
	virtual const char* Transcode (const char* Source, CString & strRet);
	virtual bool Transcode (const char* Source, unsigned short& numRet);
	virtual bool Transcode (const char* Source, short& numRet);
	virtual bool Transcode (const char* Source, int& numRet);
	virtual bool Transcode (const char* Source, unsigned int& numRet);
	virtual bool Transcode (const char* Source, bool& flagRet);
	virtual bool Transcode (const char* Source, string& strRet);

	const char* NoTranscode(const char* Source,CString& strRet) ;
	SQLiteDB* GetDB()
	{
		return &m_db; 
	}
private:
	CppSQLite3DB m_db;
	CString m_WorkDirectory;
	CString m_LogFileName;
	bool m_IsOpen;
	LogCallBack  m_LogCallBack;
};



class TUTIL_API CDBSQLite:public CSQLiteBase
{
public:
	CDBSQLite(void);
	~CDBSQLite(void);

public:
  
	//sCondition we can write as this: FIELD1 = 12 and FIELD2 = 'test';
	bool DBQueryTableFieldList(const char* sFieldName, const char*, CDBSQLite::SQLiteList& strRetList); 

	bool DBQueryTableField(const char* sFieldName, const char* sCondition, CString& strRet);

	unsigned int DBQueryTableCount();
public:
	
	bool SetTableName(const char* szTableName);

	//return the change row
	bool ExecuteSQL(const char* szSQLCommand);



protected:


private:

	CString m_TableName;
};
#endif
